//
//  BaseSqlViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/12/9.
//  Copyright © 2015年 ltz. All rights reserved.
//

#import "BaseSqlViewController.h"
#import "Contact.h"
#import "ContactTool.h"
@interface BaseSqlViewController ()
{

    UISearchBar * searchBar;
}
@property (nonatomic, strong) NSMutableArray *contacts;

@end

@implementation BaseSqlViewController


- (NSMutableArray *)contacts
{
    if (_contacts == nil) {
        // 从数据库读取数据
        _contacts = [ContactTool contacts];
        
        if (_contacts == nil) {
            _contacts = [NSMutableArray array];
        }
    }
    return _contacts;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xfffffff];
    [self showNavigationRightButton:@"添加" tintColor:0xfffffff];
   searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    self.navigationItem.titleView = searchBar;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];

    [self.view addGestureRecognizer:singleTap];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)navigationRightButtonClick:(id)sender{
    NSLog(@"ddd");
    NSArray *nameArr = @[@"gogoing",@"duang",@"uzi",@"omg"];
    
    NSString *name = [NSString stringWithFormat:@"%@%d",nameArr[arc4random_uniform(4)],arc4random_uniform(200)];
    NSString *phone = [NSString stringWithFormat:@"%d",arc4random_uniform(10000)+10000];
    Contact *c = [Contact contactWithName:name phone:phone];
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.contacts.count inSection:0];
    [self.contacts addObject:c];
    
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // 存储到数据库里面
    [ContactTool saveWithContact:c];
    
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // o  select * from t_contact where name like '%searchText%' or phone like '%searchText%'
    // % 在stringWithFormat中有特殊意思
    // %% == %
    // 输入一个文字，进行模糊查询，查看下名字或者电话是否包含文字
    NSString *sql = [NSString stringWithFormat:@"select * from t_contact where name like '%%%@%%' or phone like '%%%@%%';",searchText,searchText];
    _contacts = (NSMutableArray *)[ContactTool contactWithSql:sql];

    [self.tableView reloadData];
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    // 给服务器发送请求
    
    // 刷新表格
    
    NSLog(@"%s",__func__);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contacts.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseId = @"ContractWorkTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    Contact *c  = self.contacts[indexPath.row];
    cell.textLabel.text = c.name;
    cell.detailTextLabel.text = c.phone;
    return cell;
}

#pragma mark  数据源方法 提交编辑操作时调用 自动实现左划删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(editingStyle ==UITableViewCellEditingStyleDelete){
        
       Contact *c= _contacts[indexPath.row];
        NSString * stringSql=[NSString stringWithFormat:@"delete from t_contact where id = %d",c.contactId];
    BOOL flag=    [ContactTool execWithSql:stringSql];
        
        if (flag) {
            [_contacts removeObjectAtIndex:indexPath.row];
            //刷新所有数据
            //    [_tableView reloadData ];
            //有动画的删除
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
        
    }
    
    
}

//修改 左划删除 按钮
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath{
    
    return @"删除";
    
}
@end
